<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/1/18
 * Time: 9:44
 */
namespace app\controller\index;
use think\facade\View;
// 启动会话

class Base
{
    protected $page = 1;
    protected $size = 10;
    protected $start_time = '';
    protected $end_time = '';
    protected $user;
    public function __construct()
    {
        global $user;
        session_start();
        $this->page = input('page',1);
        $this->size = input('size',10);
        $this->start_time = input('start_time');
        $this->end_time = input('end_time');
        if (empty($_SESSION)) {
            header('Location:/index/login/index');
            die;
        }
        if(empty($_SESSION['username'])) {
            header('Location:/index/login/index');
            die;
        }
        $user = $this->user = \app\model\index\User::where('username',$_SESSION['username'])->find();
        View::assign('user_message',$user);
    }

    /**
     * 返回视图
     * @param string $view
     * @param array $data
     * @return string
     */
    protected function view($view = 'index', array $data = []){
        View::assign($data);
        return View::fetch($view);
    }

    protected function msg($data = [],string $msg = '成功',int $code = 200,int $http_code = 200){
        return json(['code' => $code,'msg' => $msg,'data' => $data],$http_code);
    }

    protected function fail($msg = 'fail',$code = 400,$http_code = 200){
        return json(['code' => $code,'msg' => $msg,'data' => (object)[]],$http_code);
    }
}